<?php
define('APPFORUM','1.00');
/************************************************************************/
/* FrameWork  test preview                                              */
/* ==================================================================== */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/

// carica libemoticons e libBBcode se sono presenti
loadlib("libBBcode");
loadlib("libEmoticons");


//carica la lingua se esiste
$lang=lang();
if (file_exists("applications/$appname/lang/$lang.inc"))
    include "applications/$appname/lang/$lang.inc";
else
    include "applications/$appname/lang/it.inc";

/* Forum Application */
//-------------------------------------------------------------------------------------------------------------------------

if (!file_exists("datas/$path"))mkdir("datas/$path",0755);
if (!file_exists("datas/$path/$apptitle"))mkdir("datas/$path/$apptitle",0755);

$forums_path="datas/$path/$apptitle";
//-------------------------------------------------------------------------------------------------------------------------
//incos_path
function icons_path($path){
    $tema=theme();
    if (file_exists("themes/$tema/forum/images/"))
	return "themes/$tema/forum/images/";
    else
	return "$path/".appname()."/images/";
}
//-------------------------------------------------------------------------------------------------------------------------
// conta forums
function forums_count($forums_path,$path,$appname){
    $handle=opendir($forums_path);
	$forums_count=0;
    while ($file = readdir($handle)) {
	if (is_dir("$forums_path/$file") && $file[0]!='.' ){
	$forums_count++;
	}
    }
    closedir($handle);
    return $forums_count;

}
//-------------------------------------------------------------------------------------------------------------------------
// elenca forums
function forums_list($forums_path,$path,$appname){
    $handle=opendir($forums_path);
    while ($forum = readdir($handle)) {

	if (is_dir("$forums_path/$forum") && $forum[0]!='.' ){
	    $name=str_replace("_"," ",$forum);
	    echo "<table id='news' class='class_table'>";
	    echo "<tr class='firstrow'><td colspan='2'><b><IMG src='".icons_path($path)."folder.gif' border=0 > $name</b></td></tr>";
	    echo "<tr>";
	    echo "<td >".@join(@file("$forums_path/$forum/description.txt"))."</td>";
	    echo "<td width='100'><a href=".ahah_link("index.php?appname=$appname&forum=$forum&op=topics_list",$path,"center").">"._ENTRA."</a> <IMG src='".icons_path($path)."icon_mini_register.gif' border=0 > </td>";
	    echo "</tr>";
	    echo "<tr>";
	    echo "<td ><b>".topics_count($forums_path,$forum,$path,$appname)."</b> "._TOPICS." | "._ULTIMO." <b>".last_topic($forums_path,$forum,$path,$appname)."</b></td><td></td>";
	    echo "</tr>";
	    echo "</table><br />";
	    echo "<hr />";
	    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP()){
		echo "<h3 class='edit'>";
		echo "<a href=".ahah_link("index.php?appname=$appname&forum=$forum&op=forum_modify",$path,"center")."> <IMG src='".icons_path($path)."icon_minipost.gif' border=0 > "._FORUM_MODIFY."</a>";
		if (topics_count($forums_path,$forum,$path,$appname)==0){
		    echo " | ";
		    echo "<a href=".ahah_link("index.php?appname=$appname&forum=$forum&op=forum_delete",$path,"center")."> <IMG src='".icons_path($path)."icon_delete.gif' border=0 > "._FORUM_DELETE."</a>";
		}
		echo "</h3>";
		echo "<hr/>";
	    }
	}
    }
    closedir($handle);
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP()){
	echo"<h3 class='edit'><a href=".ahah_link("index.php?appname=$appname&op=forum_new",$path,"center")."> <IMG src='".icons_path($path)."folder_sticky_new.gif' border=0 > "._FORUM_NEW."</a></h3>";
    }
}
//-------------------------------------------------------------------------------------------------------------------------

// nuovo forum
function forum_new($forums_path,$path,$appname){
    echo "<form name='forum_new' action='index.php?appname=$appname&op=exec_forum_new' method='POST'>";
    echo " "._NOME_DEL_FORUM." <input type='text' name='name' value='' style=\"width:100%;\" /><br />";
    echo " "._DESCRIZIONE_FORUM." <br />";
    if (defined('LIBBBCODE'))creatoolbar("document.forum_new.description");
    echo "<textarea name='description' style=\"width:100%;height:200;\" ></textarea><br />";
    echo "<input type='submit' value='"._CREA."' />";
    echo "</form>";
    echo"<hr />";

}

function exec_forum_new($forums_path,$path,$appname){
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP()){
	admin_writelog("forum:new forum");

	$name=stripslashes($_POST['name']);
	$description=stripslashes($_POST['description']);

	$description=str_replace("<script","",$description);
	$description=str_replace("</script","",$description);
	$description=str_replace("<?","",$description);
	$description=str_replace("?>","",$description);
	$description=str_replace("\r","",$description);
	$description=str_replace("\n","<br />",$description);
	if (defined('LIBBBCODE'))$description=bb2html($description);
	if (defined('LIBEMOTICONS'))$description=emoticons2html($description);

	if ($name!=""){
	    $name=str_replace(" ","_",$name);
	    mkdir("$forums_path/$name",0755);
	    $f=fopen("$forums_path/$name/description.txt","w");
	    fwrite($f,$description);
	    fclose($f);
	    echo" <b> "._FORUM_CREATO."</b><br />";
	}
	else{
	    echo" <b> "._CREAZIONE_FORUM_IMPOSSIBILE."</b><br />";
	}

	echo "<h3 class='edit'><a href='index.php?appname=$appname'>"._CONTINUA."</a></h3>";

	echo"<hr />";
    }
}

//-------------------------------------------------------------------------------------------------------------------------
// modifica forum
function forum_modify($forums_path,$path,$appname){
    $forum=$_GET['forum'];
    $forum=str_replace("..","",$forum);
    $description=@join(@file("$forums_path/$forum/description.txt"));
    $forum=str_replace("_"," ",$forum);
    if (defined('LIBEMOTICONS'))$description=html2emoticons($description);
    if (defined('LIBBBCODE'))$description=html2bb($description);


    echo "<form name='forum_modify' action='index.php?appname=$appname&op=exec_forum_modify' method='POST'>";
    echo "<input type='hidden' name='oldname' value='$forum' />";
    echo " "._NOME_DEL_FORUM." <input type='text' name='name' value='$forum' style=\"width:100%;\" /><br />";
    echo " "._DESCRIZIONE_FORUM." <br />";
    if (defined('LIBBBCODE'))creatoolbar("document.forum_modify.description");
    echo "<textarea name='description' style=\"width:100%;height:200;\" >$description</textarea><br />";
    echo "<input type='submit' value='"._MODIFICA."' />";
    echo "</form>";
    echo"<hr />";
}

function exec_forum_modify($forums_path,$path,$appname){
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP()){
	admin_writelog("forum:modify forum");


	$name=stripslashes($_POST['name']);
	$oldname=stripslashes($_POST['oldname']);

	$description=stripslashes($_POST['description']);
	$description=str_replace("<script","",$description);
	$description=str_replace("</script","",$description);
	$description=str_replace("<?","",$description);
	$description=str_replace("?>","",$description);
	$description=str_replace("\r","",$description);
	$description=str_replace("\n","<br />",$description);
	if (defined('LIBBBCODE'))$description=bb2html($description);
	if (defined('LIBEMOTICONS'))$description=emoticons2html($description);

	if ($name!=""){
	    $oldname=str_replace(" ","_",$oldname);
	    $name=str_replace(" ","_",$name);
	    rename("$forums_path/$oldname","$forums_path/$name");
	    $f=fopen("$forums_path/$name/description.txt","w");
	    fwrite($f,$description);
	    fclose($f);
	    echo" <b> "._FORUM_MODIFICATO."</b><br />";
	}
	else{
	    echo "<b>"._MODIFICA_FORUM_IMPOSSIBILE."</b><br />";
	}
	echo "<h3 class='edit'><a href='index.php?appname=$appname'>"._CONTINUA."</a></h3>";
	echo"<hr />";
    }
}
//-------------------------------------------------------------------------------------------------------------------------
// elimina forums
function forum_delete($forums_path,$path,$appname){
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP()){
    	admin_writelog("forum:delete forum");


	$forum=$_GET['forum'];
	$forum=str_replace("..","",$forum);

	if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP()){
	    if (file_exists("$forums_path/$forum/description.txt")){
		unlink("$forums_path/$forum/description.txt");
		if (!rmdir("$forums_path/$forum"))
		    echo "<b>"._IMPOSIIBILE_ELIMINARE_IL_FORUM."</b><br />";
		else
		    echo "<b>"._FORUM_ELIMINATO."</b><br />";
	    }
	    else{
		echo "<b>"._IMPOSIIBILE_ELIMINARE_IL_FORUM."</b><br />";
	    }

	}
	echo "<h3 class='edit'><a href='index.php?appname=$appname>"._CONTINUA."</a></h3>";

	echo"<hr />";
    }
}
//-------------------------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------------------------
// ultimo topic modificato
function last_topic($forums_path,$forum,$path,$appname){
    $handle=opendir("$forums_path/$forum");
	$topics_count=0;
    while ($file = readdir($handle)) {
	if (substr($file,-3)=="xml"){
	    $lastpost["$file"]=filemtime("$forums_path/$forum/$file");
	}
    }
    closedir($handle);
    if(count($lastpost)){
	arsort($lastpost);
	return post_last($forums_path, $path, $appname, $forum, key($lastpost))." ".poster_last($forums_path, $path, $appname, $forum, key($lastpost));
    }
    else
	return _NESSUN_TOPIC;
}
//-------------------------------------------------------------------------------------------------------------------------
// conta topics
function topics_count($forums_path,$forum,$path,$appname){
    $handle=opendir("$forums_path/$forum");
	$topics_count=0;
    while ($file = readdir($handle)) {
	if (substr($file,-3)=="xml"){
	$topics_count++;
	}
    }
    closedir($handle);
    return $topics_count;
}
//-------------------------------------------------------------------------------------------------------------------------
// elenca topics
function topics_list($forums_path, $path, $appname){

    $forum=$_GET['forum'];
    $forum=str_replace("..","",$forum);
    $page=$_GET['page'];
    if ($page=="")$page=0;

    echo"<a href=".ahah_link("index.php?appname=$appname",$path,"center")."> <IMG src='".icons_path($path)."folder.gif' border=0 > "._FORUMS_LIST."</a><br />";
    $handle=opendir("$forums_path/$forum");
    while ($file = readdir($handle)) {
	if (substr($file,-3)=="xml"){
	    $lastpost["$file"]=filemtime("$forums_path/$forum/$file");
	}
    }
    closedir($handle);
    if(count($lastpost))
	arsort($lastpost);

    if(count($lastpost)){

	echo "<b>"._PAGINE."</b>";
	for ($i=0;$i<(count($lastpost)/10);$i++){
	    if ($i!=$page)
		echo " <a href=".ahah_link("index.php?appname=$appname&op=topics_list&forum=$forum&page=$i",$path,"center").">".($i+1)."</a> ";
	    else
		echo " ".($i+1)." ";
	}
	echo "<br />";

	$startpost=$page*10;
	$i=0;
	foreach($lastpost as $file =>$timepost){
	    $i++;
	    if ($i>$startpost && $i<$startpost+10){
		$string=@join(@file("$forums_path/$forum/$file"));
		$topic=ereg_replace(".*<ff:topic>","",$string);
		$topic=ereg_replace("</ff:topic>.*","",$topic);

		echo "<table id='news' class='class_table' >";
		echo "<tr class='firstrow'><td colspan='2'><b><IMG src='".icons_path($path)."icon_mini_register.gif' border=0 > $topic</b></td></tr>";
		echo "<tr>";
		echo "<td >"._MESSAGGI."<b> ".posts_count($forums_path, $path, $appname, $forum, $file )."</b> | "._ULTIMO," <b>".post_last($forums_path, $path, $appname, $forum, $file )." ".poster_last($forums_path, $path, $appname, $forum, $file )."</b></td>";
		echo "<td width='100' ><a href=".ahah_link("index.php?appname=$appname&op=posts_list&forum=$forum&file=$file",$path,"center").">"._LEGGI." <IMG src='".icons_path($path)."icon_mini_register.gif' border=0 > </a></td>";
		echo "</tr>";
		echo "</table><br />";
		echo "<hr />";
		if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP()){
		    echo "<h3 class='edit'>";
		    echo "<a href=".ahah_link("index.php?appname=$appname&forum=$forum&file=$file&op=topic_modify",$path,"center")."> <IMG src='".icons_path($path)."icon_minipost.gif' border=0 > "._TOPIC_MODIFY."</a>";
		    echo " | ";
		    echo "<a href=".ahah_link("index.php?appname=$appname&forum=$forum&file=$file&op=topic_delete",$path,"center")."> <IMG src='".icons_path($path)."topic_delete.gif' border=0 > "._TOPIC_DELETE."</a>";
		    echo "</h3>";
		    echo "<hr/>";
		}
	    }
	}
	echo "<IMG src='".icons_path($path)."folder.gif' border=0 > <b>"._PAGINE."</b>";
	for ($i=0;$i<(count($lastpost)/10);$i++){
	    if ($i!=$page)
		echo " <a href=".ahah_link("index.php?appname=$appname&op=topics_list&forum=$forum&page=$i",$path,"center").">".($i+1)."</a> ";
	    else
		echo " ".($i+1)." ";
	}
	echo "<br />";


    }
    if (user_getuser()){
	echo"<h3 class='edit'><a href=".ahah_link("index.php?appname=$appname&forum=$forum&op=topics_new",$path,"center")."> "._TOPIC_NEW." <IMG src='".icons_path($path)."post.gif' border=0 > </a></h3>";
    }
}
//-------------------------------------------------------------------------------------------------------------------------
// nuovo topic
function topics_new($forums_path, $path, $appname){
    $forum=$_GET['forum'];
    $forum=str_replace("..","",$forum);
    echo "<form name='topic_new' action='index.php?appname=$appname&forum=$forum&op=exec_topic_new' method='POST' >";
    echo _TOPIC.": <input type='text' name='topic' style=\"width:100%;\" /><br />";
    if (defined('LIBBBCODE'))creatoolbar("document.topic_new.body");
    echo "<textarea name='body' style='width:100%;height:200px;' ></textarea><br />";
    echo"<input type='submit' value='"._CREA."' />";
    echo"</form><hr />";
}
function exec_topic_new($forums_path, $path, $appname){
    if (user_getuser()){

	$forum=$_GET['forum'];
	$forum=str_replace("..","",$forum);
	$user=user_getuser();

	$topic=stripslashes($_POST['topic']);
	$body=stripslashes($_POST['body']);

	$body=str_replace("<script","",$body);
	$body=str_replace("</script","",$body);
	$body=str_replace("<?","",$body);
	$body=str_replace("?>","",$body);
	$body=str_replace("\r","",$body);
	$body=str_replace("\n","<br />",$body);
	if (defined('LIBBBCODE'))$body=bb2html($body);
	if (defined('LIBEMOTICONS'))$body=emoticons2html($body);


	if ($topic!=""){
	    $file=time().".xml";
	    $f=fopen("$forums_path/$forum/$file","w");
	    fwrite($f,"<?xml version='1.0'?>
<!DOCTYPE ff:forum SYSTEM \"http://flatnuke.sourceforge.net/dtd/forum.dtd\">
<ff:forum xmlns:ff=\"http://flatnuke.sourceforge.net/forum\">
	<ff:topic>$topic</ff:topic>
	<ff:posts>
		<ff:post>
			<ff:poster>$user</ff:poster>
			<ff:subj>$topic</ff:subj>
			<ff:postbody>$body</ff:postbody>
			<ff:postime>".time()."</ff:postime>
		</ff:post>
	</ff:posts>
</ff:forum>");
	    fclose($f);
	    echo "<b>"._TOPIC_CREATO."</b><br />";
	}
	else{
	    echo "<b>"._CREAZIONE_TOPIC_IMPOSSIBILE."</b><br />";
	}
	echo "<h3 class='edit'><a href='index.php?appname=$appname&forum=$forum&topic=$topic&file=$file&op=posts_list'>"._CONTINUA."</a></h3>";
	echo"<hr />";
    }
}
//-------------------------------------------------------------------------------------------------------------------------
// modifca topic
function topic_modify($forums_path, $path, $appname){
    $forum=$_GET['forum'];
    $forum=str_replace("..","",$forum);

    $file=$_GET['file'];
    $file=str_replace("..","",$file);

    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP()){
	if (file_exists("$forums_path/$forum/$file")){
	    $string=join(file("$forums_path/$forum/$file"));

	    $topic=ereg_replace(".*<ff:topic>","",$string);
	    $topic=ereg_replace("</ff:topic>.*","",$topic);

	    echo "<form name='topic_modify' action='index.php?appname=$appname&forum=$forum&file=$file&op=exec_topic_modify' method='POST' >\n";
	    echo _TOPIC.": <input type='text' name='topic' value='$topic' style=\"width:100%;\" /><br />\n";
	    echo" <input type='hidden' name='string' value='".urlencode($string)."' />\n";
	    echo" <input type='submit' value='"._MODIFICA."' />\n";
	    echo"</form><br />\n";
	}
	else{
	    echo "<b>"._IMPOSSIBILE_MODIFICARE_IL_TOPIC."</b><br />";
	    echo "<h3 class='edit'><a href='index.php?appname=$appname&forum=$forum&topic=$topic&file=$file&op=posts_list'>"._CONTINUA."</a></h3>";
	}
    }
    echo "<hr />";
}

function exec_topic_modify($forums_path, $path, $appname){
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP()){

	$forum=$_GET['forum'];
	$forum=str_replace("..","",$forum);

	$file=$_GET['file'];
	$file=str_replace("..","",$file);


	$string=urldecode($_POST['string']);
	$topic=$_POST['topic'];

	if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP()){
	    if (file_exists("$forums_path/$forum/$file")){
		$string=ereg_replace("<ff:topic>.*.</ff:topic>","<ff:topic>$topic</ff:topic>",$string);
		$fp=fopen("$forums_path/$forum/$file","w");
		fwrite($fp,$string);
		fclose($fp);
		echo "<b>"._TOPIC_MODIFICATO."</b><br />";

	    }
	    else{
		echo "<b>"._IMPOSSIBILE_MODIFICARE_IL_TOPIC."</b><br />";
	    }
	}
	echo "<h3 class='edit'><a href='index.php?appname=$appname&forum=$forum&topic=$topic&op=topics_list'>"._CONTINUA."</a></h3>";
	echo"<hr />";
    }
}
//-------------------------------------------------------------------------------------------------------------------------
// elimina topic
function topic_delete($forums_path, $path, $appname){
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP()){
	admin_writelog("forum:delete topic");

	$forum=$_GET['forum'];
	$forum=str_replace("..","",$forum);
	$file=$_GET['file'];
	$file=str_replace("..","",$file);

	if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP()){
	    if (file_exists("$forums_path/$forum/$file")){
		unlink("$forums_path/$forum/$file");
		echo "<b>"._TOPIC_ELIMINATO."</b><br />";
	    }
	    else{
		echo "<b>"._IMPOSSIBILE_ELIMINARE_IL_TOPIC."</b><br />";
	    }

	}
	echo "<h3 class='edit'><a href='index.php?appname=$appname&forum=$forum&topic=$topic&op=topics_list'>"._CONTINUA."</a></h3>";
	echo"<hr />";
    }
}
//-------------------------------------------------------------------------------------------------------------------------


//-------------------------------------------------------------------------------------------------------------------------
// ultimo poster
function poster_last($forums_path, $path, $appname, $forum, $file ){
   $string=join(file("$forums_path/$forum/$file"));

    $posts=ereg_replace(".*<ff:posts>","",$string);
    $posts=ereg_replace("</ff:posts>.*","",$posts);
    $array_post= explode("</ff:post>",$posts);
    array_splice ($array_post, count($array_post)-1);

    $post=$array_post[count($array_post)-1];

    $poster=ereg_replace(".*<ff:poster>","",$post);
    $poster=ereg_replace("</ff:poster>.*","",$poster);

    return $poster;
}

//-------------------------------------------------------------------------------------------------------------------------
// ultimo post
function post_last($forums_path, $path, $appname, $forum, $file ){
   $string=join(file("$forums_path/$forum/$file"));

    $posts=ereg_replace(".*<ff:posts>","",$string);
    $posts=ereg_replace("</ff:posts>.*","",$posts);
    $array_post= explode("</ff:post>",$posts);
    array_splice ($array_post, count($array_post)-1);

    $post=$array_post[count($array_post)-1];

    $postime=ereg_replace(".*<ff:postime>","",$post);
    $postime=ereg_replace("</ff:postime>.*","",$postime);

    return date(formato_data_ora(),$postime);
}
//-------------------------------------------------------------------------------------------------------------------------
// conta post
function posts_count($forums_path, $path, $appname, $forum, $file ){

    $string=join(file("$forums_path/$forum/$file"));

    $posts=ereg_replace(".*<ff:posts>","",$string);
    $posts=ereg_replace("</ff:posts>.*","",$posts);
    $array_post= explode("</ff:post>",$posts);
    array_splice ($array_post, count($array_post)-1);
    return count($array_post);
}
//-------------------------------------------------------------------------------------------------------------------------
// elenca post
function posts_list($forums_path, $path, $appname){
    $forum=$_GET['forum'];
    $forum=str_replace("..","",$forum);
    $file=$_GET['file'];
    $file=str_replace("..","",$file);

    $page=$_GET['page'];
    if ($page=="")$page=0;


    echo"<a href=".ahah_link("index.php?appname=$appname",$path,"center")."><IMG src='".icons_path($path)."folder.gif' border=0 > "._FORUMS_LIST."</a><br />";
    echo"<a href=".ahah_link("index.php?appname=$appname&op=topics_list&forum=$forum",$path,"center")."><IMG src='".icons_path($path)."icon_mini_register.gif' border=0 >  "._TOPICS_LIST."</a><br />";
    $string=join(file("$forums_path/$forum/$file"));

    $topic=ereg_replace(".*<ff:topic>","",$string);
    $topic=ereg_replace("</ff:topic>.*","",$topic);

    $posts=ereg_replace(".*<ff:posts>","",$string);
    $posts=ereg_replace("</ff:posts>.*","",$posts);
    $array_post= explode("</ff:post>",$posts);
    array_splice ($array_post, count($array_post)-1);

    echo "<b>"._PAGINE."</b>";
    for ($i=0;$i<(count($array_post)/10);$i++){
	if ($i!=$page)
	    echo " <a href=".ahah_link("index.php?appname=$appname&op=posts_list&forum=$forum&file=$file&page=$i",$path,"center").">".($i+1)."</a> ";
	else
	    echo " ".($i+1)." ";
    }
    echo "<br />";

    $startpost=$page*10;
    for($i=0;$i<count($array_post);$i++ ){
	if ($i>=$startpost && $i<$startpost+10){
	    $post=$array_post[$i];
	    $poster=ereg_replace(".*<ff:poster>","",$post);
	    $poster=ereg_replace("</ff:poster>.*","",$poster);

	    $subj=ereg_replace(".*<ff:subj>","",$post);
	    $subj=ereg_replace("</ff:subj>.*","",$subj);

	    $postbody=ereg_replace(".*<ff:postbody>","",$post);
	    $postbody=ereg_replace("</ff:postbody>.*","",$postbody);

	    $postime=ereg_replace(".*<ff:postime>","",$post);
	    $postime=ereg_replace("</ff:postime>.*","",$postime);



	    echo "<table id='posts' class='class_table'>";
	    echo "<tr class='firstrow'>";
	    echo "<td colspan=2><b>$subj</b></td>";
	    echo "</tr>";echo "<td><b>Utente</b></td>";echo "<td>".date(formato_data_ora(),$postime)."</td>";
	    echo "<tr>";

	    $immagine=user_getkey($poster,"user_avatar");
	    if (substr($immagine,0,5)!="http:"){
		$immagine="libs/avatars/$immagine";
	    }
	    $email=user_getkey($poster,"user_email");
	    $web=user_getkey($poster,"user_web");


	    echo "<td width='120'> <center><a href='index.php?appname=reguser&subapp=_viewuser&user=$poster'><IMG src='$immagine' border='0'><br><b>$poster</b></a></center><hr><a href='index.php?appname=reguser&subapp=_viewuser&user=$poster'><IMG src='".icons_path($path)."icon_profile.gif' border=0 ></a><a href='mailto:$email'><IMG src='".icons_path($path)."icon_email.gif' border=0 ></a><br><a href='$web'><IMG src='".icons_path($path)."icon_www.gif' border=0 ></a><a href='index.php?appname=message'><IMG src='".icons_path($path)."icon_pm.gif' border=0 ></a></td>";
	    echo "<td colspan='2'>$postbody</td>";
	    echo "</tr>";
	    echo "<tr>";

	    echo "</tr>";
	    echo "</table><hr/>";
	    if (user_getuser()==$poster || ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP())){
		echo "<h3 class='edit'>";
		echo "<a href=".ahah_link("index.php?appname=$appname&forum=$forum&file=$file&postindex=$i&op=post_modify",$path,"center")."><IMG src='".icons_path($path)."icon_minipost.gif' border=0 >  "._POST_MODIFY."</a>";
		echo " | ";
		echo "<a href=".ahah_link("index.php?appname=$appname&forum=$forum&file=$file&postindex=$i&op=post_delete",$path,"center")."><IMG src='".icons_path($path)."icon_delete.gif' border=0 >  "._POST_DELETE."</a>";
		echo "</h3>";
		echo "<hr/>";
	    }
	}
    }
    echo "<IMG src='".icons_path($path)."msg_inbox.gif' border=0 >  <b>"._PAGINE."</b>";
    for ($i=0;$i<(count($array_post)/10);$i++){
	if ($i!=$page)
	    echo " <a href=".ahah_link("index.php?appname=$appname&op=posts_list&forum=$forum&file=$file&page=$i",$path,"center").">".($i+1)."</a> ";
	else
	    echo " ".($i+1)." ";
    }
    echo "<br />";

    if (user_getuser()){
	echo"<h3 class='edit'><a href=".ahah_link("index.php?appname=$appname&forum=$forum&file=$file&op=post_new",$path,"center")."> <IMG src='".icons_path($path)."icon_newest_reply.gif' border=0 > "._POST_NEW."</a></h3>";
    }

}
//-------------------------------------------------------------------------------------------------------------------------
// nuovo post
function post_new($forums_path, $path, $appname){
    $forum=$_GET['forum'];
    $forum=str_replace("..","",$forum);
    $file=$_GET['file'];
    $file=str_replace("..","",$file);


    $string=join(file("$forums_path/$forum/$file"));

    $topic=ereg_replace(".*<ff:topic>","",$string);
    $topic=ereg_replace("</ff:topic>.*","",$topic);

    echo "<h3>$topic</h3>\n";
    echo "<form name='post_new' action='index.php?appname=$appname&forum=$forum&file=$file&op=exec_post_new' method='POST' >";

    if (defined('LIBBBCODE'))creatoolbar("document.post_new.postbody");
    echo "<textarea name='postbody' style='width:100%;height:200px;'></textarea><br />\n";
    echo "<input type='submit' value='"._INVIA."' />\n";
    echo "</form><hr />\n";
    posts_list($forums_path, $path, $appname);
}

function exec_post_new($forums_path, $path, $appname){

    $poster=user_getuser();
    if ($poster=="")exit;

    $forum=$_GET['forum'];
    $forum=str_replace("..","",$forum);
    $file=$_GET['file'];
    $file=str_replace("..","",$file);

    $string=join(file("$forums_path/$forum/$file"));

    $topic=ereg_replace(".*<ff:topic>","",$string);
    $topic=ereg_replace("</ff:topic>.*","",$topic);
    $subj="Re: ".$topic;

    $postbody=$_POST['postbody'];
    $postbody=stripslashes($postbody);
    $postbody=str_replace("\r","",$postbody);
    $postbody=str_replace("\n","<br />",$postbody);
    $postbody=str_replace("<script","",$postbody);
    $postbody=str_replace("</script","",$postbody);
    $postbody=str_replace("<?","",$postbody);
    $postbody=str_replace("?>","",$postbody);
    if (defined('LIBBBCODE'))$postbody=bb2html($postbody);
    if (defined('LIBEMOTICONS'))$postbody=emoticons2html($postbody);

    $postime=time();

    $lastpost="	<ff:post>
			<ff:poster>$poster</ff:poster>
			<ff:subj>$subj</ff:subj>
			<ff:postbody>$postbody</ff:postbody>
			<ff:postime>$postime</ff:postime>
		</ff:post>
	</ff:posts>";


    $string=str_replace("</ff:posts>",$lastpost,$string);
    $fp=fopen("$forums_path/$forum/$file","w");
    fwrite($fp,$string);
    fclose($fp);

    echo "<b>"._POST_INVIATO."</b><br />";
    echo "<h3 class='edit'><a href='index.php?appname=$appname&forum=$forum&topic=$topic&file=$file&op=posts_list'>"._CONTINUA."</a></h3>";
    echo "<hr />";
}
//-------------------------------------------------------------------------------------------------------------------------
// modifica post
function post_modify($forums_path, $path, $appname){
    $forum=$_GET['forum'];
    $forum=str_replace("..","",$forum);
    $file=$_GET['file'];
    $file=str_replace("..","",$file);

    $postindex=$_GET['postindex'];

    $string=join(file("$forums_path/$forum/$file"));

    $topic=ereg_replace(".*<ff:topic>","",$string);
    $topic=ereg_replace("</ff:topic>.*","",$topic);


    $posts=ereg_replace(".*<ff:posts>","",$string);
    $posts=ereg_replace("</ff:posts>.*","",$posts);
    $array_post= explode("</ff:post>",$posts);
    array_splice ($array_post, count($array_post)-1);
    $post=$array_post[$postindex];

    $poster=ereg_replace(".*<ff:poster>","",$post);
    $poster=ereg_replace("</ff:poster>.*","",$poster);

    $subj=ereg_replace(".*<ff:subj>","",$post);
    $subj=ereg_replace("</ff:subj>.*","",$subj);

    $postbody=ereg_replace(".*<ff:postbody>","",$post);
    $postbody=ereg_replace("</ff:postbody>.*","",$postbody);
    $postbody=str_replace("<br />","\n",$postbody);
    if (defined('LIBEMOTICONS'))$postbody=html2emoticons($postbody);
    if (defined('LIBBBCODE'))$postbody=html2bb($postbody);


    $postime=ereg_replace(".*<ff:postime>","",$post);
    $postime=ereg_replace("</ff:postime>.*","",$postime);

    echo "<h3>$topic</h3>\n";
    echo "<form name='post_modify' action='index.php?appname=$appname&forum=$forum&file=$file&postindex=$postindex&op=exec_post_modify' method='POST' >";

    echo "<input type='hidden' name='poster' value='$poster' />\n";
    echo "<input type='hidden' name='subj' value='$subj' />\n";
    echo "<input type='hidden' name='postime' value='$postime' />\n";
    if (defined('LIBBBCODE'))creatoolbar("document.post_modify.postbody");
    echo "<textarea name='postbody' style='width:100%;height:200px;'>$postbody</textarea><br />\n";
    echo "<input type='submit' value='"._MODIFICA."' /><IMG src='".icons_path($path)."icon_newest_reply.gif' border=0 >\n";
    echo "</form><hr />\n";
    posts_list($forums_path, $path, $appname);
}

function exec_post_modify($forums_path, $path, $appname){
    $poster=$_POST['poster'];

    if (user_getuser()==$poster || ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP())){
    	admin_writelog("forum:post modify");


	$forum=$_GET['forum'];
	$forum=str_replace("..","",$forum);
	$file=$_GET['file'];
	$file=str_replace("..","",$file);

	$postindex=$_GET['postindex'];

	$string=join(file("$forums_path/$forum/$file"));

	$topic=ereg_replace(".*<ff:topic>","",$string);
	$topic=ereg_replace("</ff:topic>.*","",$topic);
	$subj="Re: ".$topic;

	$postbody=$_POST['postbody'];
	$postbody=stripslashes($postbody);
	$postbody=str_replace("\r","",$postbody);
	$postbody=str_replace("\n","<br />",$postbody);
	$postbody=str_replace("<script","",$postbody);
	$postbody=str_replace("</script","",$postbody);
	$postbody=str_replace("<?","",$postbody);
	$postbody=str_replace("?>","",$postbody);
	if (defined('LIBBBCODE'))$postbody=bb2html($postbody);
	if (defined('LIBEMOTICONS'))$postbody=emoticons2html($postbody);

	$postime=$_POST['postime'];

	$posts=ereg_replace(".*<ff:posts>","",$string);
	$posts=ereg_replace("</ff:posts>.*","",$posts);
	$array_post= explode("</ff:post>",$posts);
	array_splice ($array_post, count($array_post)-1);


	$lastpost="	<ff:post>
			<ff:poster>$poster</ff:poster>
			<ff:subj>$subj</ff:subj>
			<ff:postbody>$postbody</ff:postbody>
			<ff:postime>$postime</ff:postime>
		";

	$array_post[$postindex]=$lastpost;

	$posts="";
	foreach($array_post as $post)
	    $posts=$posts.$post."</ff:post>";

	$f=fopen("$forums_path/$forum/$file","w");
	fwrite($f,"<?xml version='1.0'?>
<!DOCTYPE ff:forum SYSTEM \"http://flatnuke.sourceforge.net/dtd/forum.dtd\">
<ff:forum xmlns:ff=\"http://flatnuke.sourceforge.net/forum\">
        <ff:topic>$topic</ff:topic>
	<ff:posts>
	    $posts
	</ff:posts>
</ff:forum>");
	fclose($f);

	echo "<b>"._POST_MODIFICATO."</b><br />";
	echo "<h3 class='edit'><a href='index.php?appname=$appname&forum=$forum&topic=$topic&file=$file&op=posts_list'>"._CONTINUA."</a></h3>";
	echo"<hr />";
    }
    else{
	echo "<b>"._IMPOSSIBILE_MODIFICARE_IL_POST."</b><br />";
    }
}
//-------------------------------------------------------------------------------------------------------------------------
// elimina post
function post_delete($forums_path, $path, $appname){
    $forum=$_GET['forum'];
    $forum=str_replace("..","",$forum);
    $file=$_GET['file'];
    $file=str_replace("..","",$file);

    $postindex=$_GET['postindex'];

    $string=join(file("$forums_path/$forum/$file"));

    $poster=ereg_replace(".*<ff:poster>","",$string);
    $poster=ereg_replace("</ff:poster>.*","",$poster);

    if (user_getuser()==$poster || ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_verifyIP())){
	admin_writelog("forum:delete post");


	$forum=$_GET['forum'];
	$forum=str_replace("..","",$forum);
	$file=$_GET['file'];
	$file=str_replace("..","",$file);

	$postindex=$_GET['postindex'];


	$topic=ereg_replace(".*<ff:topic>","",$string);
	$topic=ereg_replace("</ff:topic>.*","",$topic);
	$subj="Re: ".$topic;


	$posts=ereg_replace(".*<ff:posts>","",$string);
	$posts=ereg_replace("</ff:posts>.*","",$posts);
	$array_post= explode("</ff:post>",$posts);
	array_splice ($array_post, count($array_post)-1);

	unset($array_post[$postindex]);

	$posts="";
	foreach($array_post as $post)
	    $posts=$posts.$post."</ff:post>";

	$f=fopen("$forums_path/$forum/$file","w");
	fwrite($f,"<?xml version='1.0'?>
<!DOCTYPE ff:forum SYSTEM \"http://flatnuke.sourceforge.net/dtd/forum.dtd\">
<ff:forum xmlns:ff=\"http://flatnuke.sourceforge.net/forum\">
        <ff:topic>$topic</ff:topic>
	<ff:posts>$posts
	</ff:posts>
</ff:forum>");
	fclose($f);

	echo "<b>"._POST_ELIMINATO."</b><br />";
	echo "<h3 class='edit'><a href='index.php?appname=$appname&forum=$forum&topic=$topic&file=$file&op=posts_list'>"._CONTINUA."</a></h3>";
	echo "<hr />";
    }
    else{
	echo "<b>"._IMPOSSIBILE_ELIMINARE_IL_POST."</b><br />";
    }
}

//-------------------------------------------------------------------------------------------------------------------------

?>
<?php structure_openBlock("table appl",apptitle($appname),""); ?>

<p>
<?php
    $op=$_GET['op'];
    switch($op){

	case "posts_list":
	    posts_list( $forums_path,$path, $appname);
	    break;
	case "post_new":
	    post_new( $forums_path,$path, $appname);
	    break;
	case "exec_post_new":
	    exec_post_new( $forums_path,$path, $appname);
	    break;
	case "post_modify":
	    post_modify( $forums_path,$path, $appname);
	    break;
	case "exec_post_modify":
	    exec_post_modify( $forums_path,$path, $appname);
	    break;
	case "post_delete":
	    post_delete( $forums_path,$path, $appname);
	    break;

	case "topics_list":
	    topics_list( $forums_path,$path, $appname);
	    break;
	case "topics_new":
	    topics_new( $forums_path,$path, $appname);
	    break;
	case "exec_topic_new":
	    exec_topic_new( $forums_path,$path, $appname);
	    break;
	case "topic_modify":
	    topic_modify( $forums_path,$path, $appname);
	    break;
	case "exec_topic_modify":
	    exec_topic_modify( $forums_path,$path, $appname);
	    break;
	case "topic_delete":
	    topic_delete( $forums_path,$path, $appname);
	    break;

	case "forum_new":
	    forum_new( $forums_path, $path, $appname);
	    break;
	case "exec_forum_new":
	    exec_forum_new( $forums_path, $path, $appname);
	    break;
	case "forum_modify":
	    forum_modify( $forums_path, $path, $appname);
	    break;
	case "exec_forum_modify":
	    exec_forum_modify( $forums_path, $path, $appname);
	    break;
	case "forum_delete":
	    forum_delete( $forums_path, $path, $appname);
	    break;

	default:
	    forums_list( $forums_path, $path, $appname);
	    break;
  }
?>
</p>
<?php structure_closeBlock(); ?>



